package service.system.impl;
import java.util.List;
import java.util.Map;
import core.global.Global;

import model.security.UsrAuth;
import dao.security.UsrAuthDao;
import service.system.MenuService;


public class MenuServiceImpl implements MenuService {
	private UsrAuthDao authdao;

	@Override
	public String getMenuJson(String page) throws Exception{
		long userid=Global.userid();
		
		UsrAuth auth=authdao.findOne("authname", page);
		if(auth==null){
			return "[{\"attr\":{\"id\":\"10\"},\"data\":{\"title\":\"我的信息\",\"attr\":{\"href\":\"#\"}}}]";
		}
		int pageid=auth.getAuthid();		
		List menulist = getMenuBeanList(pageid,userid);
		if(menulist==null){
			return "";
		}
		
		int menulistsize=menulist.size();
		
		StringBuffer str=new StringBuffer("[");
		
		Map recMap = null;
		for(int i=0;i<menulistsize;i++){			
			recMap = (Map)menulist.get(i);
            str.append(this.jsonborder(recMap.get("authid"), recMap.get("authdesc"), recMap.get("reslink")));	   
            if ("#".equals(recMap.get("reslink"))){
        		List submenulist =getMenuBeanList(recMap.get("authid"),userid);
        		if(submenulist!=null){	        		
	        		int submenusize=submenulist.size();

	            	str.append( ",\"children\":[");	            	
	        		Map subrecMap = null;
	            	for (int j=0;j<submenusize;j++){
	            		subrecMap = (Map)submenulist.get(j);
	                    str.append(this.jsonborder(subrecMap.get("authid"), subrecMap.get("authdesc"), subrecMap.get("reslink")));
	                    str.append("}");
	                    if (j < submenusize - 1){
	                    	str.append(",");
	                    }
	            	}
	            	str.append( "]");
        		}
            }
            str.append("}");
            if (i < menulistsize - 1){
            	str.append(",");
            }
		}
		str.append("]");
		
     
        return str.toString();
    }

	private String jsonborder(Object id,Object title,Object url){
		StringBuffer str=new StringBuffer();
		str.append("{")
		.append("\"attr\":{\"id\":\"treee")
        .append(id)
        .append("\"},")
        .append("\"data\":{\"title\":\"")
        .append(title)
        .append("\",\"attr\":{\"href\":\"..")
        .append(url)
        .append("\"}}");
        return str.toString();
	}
	
	private List<?> getMenuBeanList(Object id,Long userid){
	//	String select="new vo.z.MenuBean(auth.authid,auth.parent,auth.authname,auth.authdesc,auth.reslink)";
		
		String select="auth.authid,auth.parent,auth.authname,auth.authdesc,auth.reslink";
		
		StringBuffer a=new StringBuffer();
		a.append("usr_auth as auth inner join (")
		.append("select distinct roleauth.authid from usr_user_role as userrole , usr_role_auth as roleauth ")
		.append("where userrole.userid=")
		.append(userid)
		.append(" and userrole.roleid=roleauth.roleid")
		.append(") aa on aa.authid=auth.authid and auth.parent=")
		.append(id)
		.append(" order by auth.order asc");

		return authdao.findSqlQuery(select,a.toString(),0);
	}

	public UsrAuthDao getAuthdao() {
		return authdao;
	}

	public void setAuthdao(UsrAuthDao authdao) {
		this.authdao = authdao;
		   /*for (int i = 0; i < menulistsize; i++){
    	MenuBean menu = menulist.get(i);
        // 有子节点
        if ("#".equals(menu.getUrl())){
            str.append("{attributes:{id:\"");
            str.append(menu.getId());
            str.append("\"},state:\"open\",data:\"");
            str.append(menu.getTitle());
            str.append("\" ,");
            str.append( "children:[");

            // 查出它的子节点
    		List<?> list_child=authdao.findQuery(select, "UsrAuth auth where auth.parent="+menu.getParentid());
    		List<MenuBean> submenulist = (List<MenuBean>)list_child;
    		int submenusize=submenulist.size();
    		
            // 遍历它的子节点        		
            for (int j = 0; j < submenusize; j++){                	
            	MenuBean submenu = submenulist.get(j);
                //还有子节点(递归调用)
                if ("#".equals(submenu.getUrl())){
                    //this.getJson(ac.getParentid());
                }else{
                	str.append("{attributes:{id:\"");
                	str.append(submenu.getId());
                	str.append("\"},state:\"open\",data:\"");
                	str.append(submenu.getTitle());
                	str.append("\"    }");
                    if (j < submenusize - 1){
                    	str.append(",");
                    }
                }
            }
            str.append("]   }");
            if (i <menulistsize-1){
            	str.append(",");
            }
        }
    }*/
	}
}
